package com.changgou.goods.service.impl;

import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;
    @Override
    public List<Brand> findList() {
        return brandMapper.selectAll();
    }

    @Override
    public Brand findById(Integer id) {
        //selectByPrimaryKey查询单个主键
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }

    @Override
    //注意,添加的时候需要使用事务,否则添加不成功
    @Transactional
    public void add(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    //修改品牌,也需要事务的注解
    @Override
    @Transactional
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKey(brand);
    }

    //删除也需要事务管理
    @Override
    @Transactional
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    //由于是查询,所以不需要事务
    @Override
    public List<Brand> list(Map<String, Object> searchMap) {

        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null){
            //说明有内容
            if (searchMap.get("name")!= null && !"".equals(searchMap.get("name"))){
                //模糊查询
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            if (searchMap.get("letter") != null && !"".equals(searchMap.get("letter"))){
                criteria.andEqualTo("letter",searchMap.get("letter"));
            }
        }
        List<Brand> brandList = brandMapper.selectByExample(example);
        return brandList;
    }

    //分页查询
    @Override
    public Page<Brand> findPage(int page, int size) {
        //调用PageHelper方法传递page, size
        PageHelper.startPage(page, size);
        Page<Brand> page1 = (Page<Brand>) brandMapper.selectAll();
        return page1;
    }

    //分页+条件查询
    @Override
    public Page<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
        //先设置分页
        PageHelper.startPage(page,size);
        //设置条件查询
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null){
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            if (searchMap.get("letter") != null && !"".equals(searchMap.get("letter"))){
                criteria.andEqualTo("letter",searchMap.get("letter"));
            }
        }
        Page<Brand> pageinfo = (Page<Brand>) brandMapper.selectByExample(example);
        return pageinfo;
    }
}